<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Agency;
use app\index\model\Record;
use think\Loader;
use think\Db;
/**
 * Class Agent
 * @package app\index\controller
 */
class Agent extends Controller
{

    /*
     * 代理商数据查询
     * 返回代理商数据
     */
    public function AgentIndex()
    {
        $agency    = new Agency;
        //代理商信息显示
        $AgentSel = $agency->showall();

        return view('index',['agent'=>$AgentSel]);
    }
    /**
     * 全球逆地理编码调用
     */
    function showjwd(){
        $id  = input('id');
        $jd  = input('jd');
        $wd  = input('wd');
        $jwd = $wd.','.$jd;
        $results = nibm($jwd);
        echo $jwd.'|'.$results;
    }
    /**
     * 修改代理商信息
     */
    public function mod(){
      if(isset($_POST['submit'])){
        $id     = input('id');
        $shop   = input('shop');
        $name   = input('name');
        $iphone = input('iphone');
        $email  = input('email');
        $jwd    = input('hjwd');
        $shi    = input('shi');
        $qu     = input('qu');
        $region = input('locat');
        $agency    = new Agency;
        $results = $agency->agencymod($id,$shop,$name,$iphone,$email,$region,$shi,$qu,$jwd);

        if($results){
            zsgc('修改了代理商['.$shop.']的信息');
             $this->success('操作成功','index/agent/agentindex');
        }
        else{
            $this->error('操作失败','index/agent/agentindex');
        }
      }
        $id  = input('id');
        //查询代理商信息
        $mod = Agency::get($id)->toArray();
        $mod = Db::name('agency')
                ->alias('a')
                ->where('a.id',$id)
                ->join('shopregion b','a.id=b.agencyid','left')
                ->field('a.*,b.shi,b.qu')->find();
        return view('mod',['mod'=>$mod]);
    }
    public function add(){
      if(isset($_POST['submit'])){
        $data = [
          $shop   = input('shop'),
          $name   = input('name'),
          $iphone = input('iphone'),
          $email  = input('email'),
          $jwd    = input('hjwd'),
          $shi    = input('shi'),
          $qu    = input('qu'),
          $region = input('locat')
      ];
      $validate = Loader::validate('Agent');
      if(!$validate->scene('add')->check($data)){
         $agency    = new Agency;
         $user = $agency->agencyadd($shop,$name,$iphone,$email,$jwd,$shi,$qu,$region);
      }

      if($user){
             zsgc('添加了代理商['.$shop.']');
             $this->success('操作成功','index/agent/agentindex');
        }
        else{
            $this->error('操作失败','index/agent/agentindex');
        }
      }
        return view('add');
    }
    public function deldo(){
        $id = trim(input('id'));
        if(!empty($id))
        {
          $Agency = Agency::get($id);
          $name = $Agency['shop'];
          $agent= new Agency;
          $re = $agent->agencydel($id);

          if($re==false){
              show(0,'代理商删除失败');
          }else{
              zsgc('删除了代理商['.$name.']');
              show(1,'代理商删除成功');
          }
        }
        else
        {
            $this->error('操作失败','index/agent/agentindex');
        }
    }
    //申请显示
    public function audit(){
        $agency    = new Agency;
        $state = 0;
        $showaudit = $agency->showaudit($state);
        return view('audit',['results'=>$showaudit]);
    }
    //同意操作
    public function agree(){
        $agency  = new Agency;
        $id = input('id');
        $name = Agency::get($id)->find()->toArray();
        $agreed  = $agency->agree($id);
        if($agreed){
            zsgc('通过了['.$name['name'].']成为代理商的申请');
            $this->success('操作成功','index/agent/audit');
        }else{
            $this->error('操作失败','index/agent/audit');
        }
    }
    //驳回操作
    public function disagree(){
        $agency  = new Agency;
        $reason = input('reason');
        $id = input('id');
        $name = Agency::get($id)->find()->toArray();
        $disagreed  = $agency->disagree($id,$reason);
        if($disagreed){
            zsgc('驳回了['.$name['name'].']成为代理商的申请');
            $this->success('操作成功','index/agent/audit');
        }else{
            $this->error('操作失败','index/agent/audit');
        }
    }
    //显示已驳回
    public function reject(){
        $agency    = new Agency;
        $state  = 2;
        $showreject = $agency->showaudit($state);
        return view('reject',['results'=>$showreject]);
    }
    //取消驳回
    public function abolish(){
        $agency    = new Agency;
        $id = input('id');
        $name = Agency::get($id)->find()->toArray();
        $abolish = $agency->abolish($id);
        if($abolish){
            zsgc('取消了驳回['.$name['name'].']的申请,将请求移动到待审核列');
            $this->success('操作成功','index/agent/reject');
        }else{
            $this->error('操作失败','index/agent/reject');
        }
    }
    //提现功能申请
    public function applyforwithdrawal(){
        $agency    = new Agency;
        $withdrawal = 1;
        $withdrawal = $agency->applyforwithdrawal($withdrawal);
        return view('applyforwithdrawal',['results'=>$withdrawal]);
    }
    public function agreewithdrawal(){
        $id = input('id');
        $agency    = new Agency;
        $name = Agency::get($id)->find()->toArray();
        $result = $agency->agreewithdrawal($id);
        if($result){
            zsgc('同意了['.$name['shop'].']的提现申请');
            $this->success('操作成功','index/agent/applyforwithdrawal');
        }else{
            $this->error('操作失败','index/agent/applyforwithdrawal');
        }
    }
    public function disagreewithdrawal(){
        $id = input('id');
        $reason = input('reason');
        $agency    = new Agency;
        $name = Agency::get($id)->find()->toArray();
        $result = $agency->disagreewithdrawal($id,$reason);
        if($result){
            zsgc('驳回了['.$name['shop'].']的提现申请');
            $this->success('操作成功','index/agent/applyforwithdrawal');
        }else{
            $this->error('操作失败','index/agent/applyforwithdrawal');
        }
    }
    //已驳回提现申请
    public function refused(){
        $agency    = new Agency;
        $withdrawal = 3;
        $withdrawal = $agency->applyforwithdrawal($withdrawal);
        return view('refused',['results'=>$withdrawal]);
    }
    //取消已驳回提现申请
    public function abolish2(){
        $agency    = new Agency;
        $id = input('id');
        $name = Agency::get($id)->find()->toArray();
        $abolish = $agency->abolish2($id);
        if($abolish){
            zsgc('取消了驳回['.$name['name'].']的提现申请,将请求移动到待审核列');
            $this->success('操作成功','index/agent/refused');
        }else{
            $this->error('操作失败','index/agent/refused');
        }
    }

}
